//
//  HMTabBar.swift
//  HMSlender
//
//  Created by 赵少朋赵少朋 on 17/1/16.
//  Copyright © 2017年 ITheima. All rights reserved.
//

import UIKit



class HMTabBar: UITabBar {

    
    var closure:(()->())?
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        let tabBarCenterY = self.frame.size.height * 0.5
        
       
    
        let w = UIScreen.main.bounds.width * 0.2
        
        var index = 1
        for View in subviews {
            
            if View.isKind(of: NSClassFromString("UITabBarButton")!) {
               
                View.center = CGPoint(x: CGFloat(index) * w - 0.5 * w, y: tabBarCenterY)
                
                if index >= 3 {
                    View.center = CGPoint(x: CGFloat( index) * w + 0.5 * w , y: tabBarCenterY)
                    index += 1
                    continue
                }
                index += 1
            }
        }
        
        focusBtn.center.x = self.frame.size.width * 0.5
        focusBtn.center.y = tabBarCenterY
    }
    
    required override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        addSubview(focusBtn)
        tintColor = UIColor.orange
    }
    
    
    private lazy var focusBtn: UIButton = {
    
        let focusBtn = UIButton()
        
        focusBtn.addTarget(self, action: #selector(focusBtnClick), for: UIControlEvents.touchUpInside)
        focusBtn.setImage(UIImage(named:"tabbar_compose_icon_add_highlighted"), for: UIControlState.highlighted)
            focusBtn.setImage(UIImage(named:"tabbar_compose_icon_add"), for: UIControlState.normal)
        
            focusBtn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), for: UIControlState.normal)
            focusBtn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), for: UIControlState.highlighted)
        
            focusBtn.sizeToFit()
        
        return focusBtn
    }()
    
    func focusBtnClick () {
        
        closure?()
    }
    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    

}
